# 组选择 用 , 例： .animal,span
# :nth-child(a) 父元素的第a个字节点
# span:nth-child(a) 父元素的第a个字节点  并且是span类型的
# span:nth-last-child(a)
# span:nth-of-type(a) 父元素第a个span的字元素
# span:nth-last-of-type(a) 
# p:nth-child(even)
# P:nth-child(odd)
# h3+span 选的是紧跟着h3的兄弟span节点   这个最终选的是span节点，h3只是定位 
# h3~span 选的是h3后面的所有的兄弟span节点  这个最终选的是span节点，h3只是定位 

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from changliang import LUJING
import time

wd = webdriver.Chrome(service=Service(LUJING))
wd.implicitly_wait(10)

wd.get('http://quote.eastmoney.com/stock_list.html')
def get_stock():
    list1 = wd.find_elements(By.CSS_SELECTOR,'.quotetable table tbody tr')
    content = ''
    for i in list1:
        code1 = i.find_element(By.CSS_SELECTOR,'td:nth-child(2)').text
        name = i.find_element(By.CSS_SELECTOR,'td:nth-child(3)').text
        zhangfu = i.find_element(By.CSS_SELECTOR,'td:nth-child(6)').text
        rs = f'{code1:<7}:{name:<5}:{zhangfu}\n'
        # print(rs)
        content += rs
    return content

def fanye(page):
    rs = ''
    for i in range(page):
        yema = i+1
        txt = wd.find_element(By.CSS_SELECTOR,'.gotoform input:nth-child(1)')
        txt.clear()
        txt.send_keys(yema)
        wd.find_element(By.CSS_SELECTOR,'.gotoform input:nth-child(2)').click()
        time.sleep(1)
        rs += get_stock()
    print(rs)
    return rs

rs = fanye(10)
with open('/Users/show/Downloads/编程/python学习/selenium_stock_info.txt','w',encoding='utf8') as f:
    f.write(rs)